#include <stdio.h>
#include <stdlib.h>
#include <string.h>


long readfile(char *filename, char *text[], int alphabet_size)
{
    FILE *fin;
    long len;
    int i = 0;
    char c;

    if (!(fin = fopen(filename, "r"))) {
        fprintf (stderr, "I can't open the file: %s\n", filename);
        exit(1);
    }
    // llegim el nombre de caracters que hi ha al fitxer
    fseek(fin, 0, SEEK_END);
    len = ftell(fin);
    fseek(fin, 0, SEEK_SET);
    // demanem memoria per la variable text
    *text = malloc(len + 1);
    // bucle per llegir el fitxer
    while ((c = (fgetc(fin))) != EOF) {
        switch(alphabet_size) {
            case 8:
                if (c=='7') {
                    (*text)[i] = c;
                    i++;
                }
            case 7:
                if (c=='6') {
                    (*text)[i] = c;
                    i++;
                }
            case 6:
                if ((c=='0')||(c=='1')||(c=='2')||(c=='3')||(c=='4')||(c=='5')) {
                    (*text)[i] = c;
                    i++;
                }
            default:
            break;
        }
    }
    fclose(fin);
    return i;
}
